//! # 指标监控模块
//! 
//! 提供流处理的指标收集和监控功能

#[cfg(feature = "metrics")]
use rustcloud_metrics::prelude::*;

/// 流处理指标收集器
#[cfg(feature = "metrics")]
#[derive(Debug)]
pub struct StreamMetrics {
    /// 处理消息计数器
    pub messages_processed: Counter,
    /// 处理失败计数器  
    pub processing_failures: Counter,
    /// 处理延迟直方图
    pub processing_latency: Histogram,
    /// 活跃流数量
    pub active_streams: Gauge,
}

#[cfg(feature = "metrics")]
impl StreamMetrics {
    /// 创建新的指标收集器
    pub fn new() -> Self {
        Self {
            messages_processed: Counter::new("stream_messages_processed_total", "处理的消息总数"),
            processing_failures: Counter::new("stream_processing_failures_total", "处理失败的消息总数"),
            processing_latency: Histogram::new("stream_processing_latency_seconds", "消息处理延迟"),
            active_streams: Gauge::new("stream_active_streams", "活跃的流数量"),
        }
    }

    /// 记录消息处理成功
    pub fn record_message_processed(&self) {
        self.messages_processed.inc();
    }

    /// 记录处理失败
    pub fn record_processing_failure(&self) {
        self.processing_failures.inc();
    }

    /// 记录处理延迟
    pub fn record_processing_latency(&self, latency_seconds: f64) {
        self.processing_latency.observe(latency_seconds);
    }

    /// 设置活跃流数量
    pub fn set_active_streams(&self, count: f64) {
        self.active_streams.set(count);
    }
}

/// 默认的空指标实现（当未启用 metrics feature 时）
#[cfg(not(feature = "metrics"))]
#[derive(Debug)]
pub struct StreamMetrics;

#[cfg(not(feature = "metrics"))]
impl StreamMetrics {
    pub fn new() -> Self {
        Self
    }

    pub fn record_message_processed(&self) {}
    pub fn record_processing_failure(&self) {}
    pub fn record_processing_latency(&self, _latency_seconds: f64) {}
    pub fn set_active_streams(&self, _count: f64) {}
}